package com.sym.nestscrollview;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.FloatRange;
import androidx.annotation.Nullable;

/**
 * @author : YOULU  sym
 * date   : 2021/3/8 0008
 * desc   : 自定义进度条
 */
public class ExamProgressBar extends View {


    /**
     * 宽度
     */
    private int mWidth;

    /**
     * 高度
     */
    private int mHeight;

    /**
     * 画笔
     */
    private Paint mPaint;

    /**
     * 进度
     */
    private float mRatio;

    /**
     * 渐变颜色范围
     */
    private final int[] mColors = {0xffFF3334, 0xffFF8B73};

    /**
     * 位置
     */
    private float mStartX, mStartY, mStopX, mStopY;

    private int mBgColor;

    private float mRadius;

    private BlurMaskFilter mBlur;


    public ExamProgressBar(Context context) {
        super(context);
    }

    public ExamProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mWidth = w;
        mHeight = h;
        init();
    }

    private void init() {

        mPaint = new Paint();
        mRadius = mHeight / 2.0f;
        mStartX = mRadius;
        mStartY = mRadius;
        mStopX = mWidth - mRadius;
        mStopY = mRadius;
        mBgColor = Color.parseColor("#F4F5F7");
        mBlur = new BlurMaskFilter(mRadius, BlurMaskFilter.Blur.SOLID);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.reset();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth((float) mHeight);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mBgColor);
        canvas.drawLine(mStartX, mStartY, mStopX, mStopY, mPaint);

        mPaint.reset();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth((float) mHeight);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setMaskFilter(mBlur);
        LinearGradient linearGradient = new LinearGradient(0, 0, mWidth * mRatio, 0, mColors, null, Shader.TileMode.CLAMP);
        mPaint.setShader(linearGradient);


        float stopX = mWidth * mRatio - mRadius;
        if (stopX <= mRadius) {

            canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
        } else {

            canvas.drawLine(mStartX, mStartY, stopX, mStopY, mPaint);
        }
    }

    public void updateProgress(@FloatRange(from = 0.0f, to = 1.0f) float ratio) {

        mRatio = ratio;
        invalidate();
    }

    public void setMax(int max) {
        mMax = max;
    }

    private int mP, mMax;

    public int getProgress() {

        return mP;
    }

    public void setProgress(int progress) {

        if (0 == mMax) {
            throw new IllegalArgumentException("请先设置最大值");
        }
        mP = progress;
        mRatio = (float) progress / mMax;
        invalidate();
    }
}
